package org.example.util;


import org.example.task.IExecutable;
import org.example.task.ITask;
import org.example.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

public class ThreadUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadUtil.class);

    public static void submitRunnable(Runnable runnable) {
        ForkJoinPool.commonPool().submit(runnable);
    }

    public static void submitTask(ITask task) {
        ForkJoinPool.commonPool().submit(() -> {
            task.execute();
            task.reportStatus();
        });
    }

    public static void submitTask(IExecutable executable) {
        ForkJoinPool.commonPool().submit(() -> {
            Result<Object> result = executable.execute();
            LOGGER.info("{} submitTask result:{}", executable,result);
        });
    }

    /**
     * 线程等待，秒
     *
     * @param seconds
     */
    public static void threadWaitSeconds(int seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            LOGGER.error(e.getMessage(), e);
            Thread.currentThread().interrupt();
        }
    }

}
